package com.ruoyi.project.access.service;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.access.domain.*;
import com.ruoyi.project.access.mapper.EnterpriseBasicInfoMapper;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;

@Service
public class EnterpriseBasicInfoService {
    @Autowired
    private EnterpriseBasicInfoMapper enterpriseBasicInfoMapper;

    ZonedDateTime currentTime = ZonedDateTime.now(ZoneId.systemDefault());
    int nowYear = currentTime.getYear();

    public EnterpriseBasicInfos getEnterpriseBasicInfosByNameToPicture(String chainType, String scenario, String product){
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String userName = user.getUserName();
        List<EnterpriseBasicInfo> newEnterpriseBasicInfoList = enterpriseBasicInfoMapper.getEnterpriseBasicInfosByNameToPicture(chainType, scenario, product);
        List<EnterpriseBasicInfo> enterpriseBasicInfoList = new ArrayList<>();
        for (EnterpriseBasicInfo enterpriseBasicInfo : newEnterpriseBasicInfoList) {
            long time = 1000*enterpriseBasicInfo.getEventTime();
            Instant instant = Instant.ofEpochMilli(time);
            ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
            int year = zonedDateTime.getYear();
            if (nowYear == year && Objects.equals(userName, "admin")) {
                enterpriseBasicInfoList.add(enterpriseBasicInfo);
            }
        }
        EnterpriseBasicInfos enterpriseBasicInfosByYear = new EnterpriseBasicInfos();
        enterpriseBasicInfosByYear.setEnterpriseBasicInfos(enterpriseBasicInfoList);
        enterpriseBasicInfosByYear.setTotal(enterpriseBasicInfoList.size());
        return enterpriseBasicInfosByYear;
    }

    public HumanResourceInfos getHumanResourceInfoByNameToPicture(String chainType, String scenario, String product){
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String userName = user.getUserName();
        List<HumanResourceInfo> humanResourceInfoList = enterpriseBasicInfoMapper.getHumanResourceInfosByNameToPicture(chainType, scenario, product);
        List<HumanResourceInfo> newHumanResourceInfoList = new ArrayList<>();
        for (HumanResourceInfo humanResourceInfo : humanResourceInfoList){
            long time = 1000*humanResourceInfo.getEventTime();
            Instant instant = Instant.ofEpochMilli(time);
            ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
            int year = zonedDateTime.getYear();
            if(nowYear == year && Objects.equals(userName, "admin")){
                newHumanResourceInfoList.add(humanResourceInfo);
            }
        }
        HumanResourceInfos humanResourceInfos = new HumanResourceInfos();
        humanResourceInfos.setHumanResourceInfos(newHumanResourceInfoList);
        humanResourceInfos.setTotal(newHumanResourceInfoList.size());
        return humanResourceInfos;
    }

    public BusinessStatusInfos getBusinessStatusInfosByNameToPicture(String chainType, String scenario, String product){
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String userName = user.getUserName();
        List<BusinessStatusInfo> businessStatusInfoList = enterpriseBasicInfoMapper.getBusinessStatusInfosByNameToPicture(chainType, scenario, product);
        List<BusinessStatusInfo> newBusinessStatusInfoList = new ArrayList<>();
        for(BusinessStatusInfo businessStatusInfo : businessStatusInfoList){
            long time = 1000*businessStatusInfo.getEventTime();
            Instant instant = Instant.ofEpochMilli(time);
            ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
            int year = zonedDateTime.getYear();
            if(nowYear == year && Objects.equals(userName, "admin")){
                newBusinessStatusInfoList.add(businessStatusInfo);
            }
        }
        BusinessStatusInfos businessStatusInfos = new BusinessStatusInfos();
        businessStatusInfos.setBusinessStatusInfos(newBusinessStatusInfoList);
        businessStatusInfos.setTotal(newBusinessStatusInfoList.size());
        return businessStatusInfos;
    }

    public void addEnterpriseBasicInfos(List<EnterpriseBasicInfo> enterpriseBasicInfos) {
        long updateTime = System.currentTimeMillis() / 1000;
        enterpriseBasicInfos.forEach(req -> req.setUpdateTime(updateTime));
        enterpriseBasicInfoMapper.addEnterpriseBasicInfos(enterpriseBasicInfos);
    }

    public EnterpriseBasicInfos getEnterpriseBasicInfos(int limit, int offset, String chainType, String scenario, String product, String statement) {
        EnterpriseBasicInfos enterpriseBasicInfos = new EnterpriseBasicInfos();
        enterpriseBasicInfos.setEnterpriseBasicInfos(enterpriseBasicInfoMapper.getEnterpriseBasicInfos(limit, offset, chainType, scenario, product, statement));
        enterpriseBasicInfos.setTotal(enterpriseBasicInfoMapper.getTotalEnterpriseBasicInfos(chainType, scenario, product, statement));
        return enterpriseBasicInfos;
    }

    public EnterpriseBasicInfos getEnterpriseBasicInfosByName(int limit, int offset, String chainType, String scenario, String product, String statement) {
        EnterpriseBasicInfos enterpriseBasicInfos = new EnterpriseBasicInfos();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String userName = user.getUserName();
        System.out.println(userName);
        if(Objects.equals(userName, "admin")) {//是系统用户
            enterpriseBasicInfos.setEnterpriseBasicInfos(enterpriseBasicInfoMapper.getEnterpriseBasicInfos(limit, offset, chainType, scenario, product, statement));
            enterpriseBasicInfos.setTotal(enterpriseBasicInfoMapper.getTotalEnterpriseBasicInfos(chainType, scenario, product, statement));
            return enterpriseBasicInfos;
        }
        else{
            enterpriseBasicInfos.setEnterpriseBasicInfos(enterpriseBasicInfoMapper.getEnterpriseBasicInfosByName(limit, offset, chainType, scenario, product, statement,userName));
            enterpriseBasicInfos.setTotal(enterpriseBasicInfoMapper.getTotalEnterpriseBasicInfosByName(chainType, scenario, product, statement,userName));
            return enterpriseBasicInfos;
        }
    }

    public HumanResourceInfos getHumanResourceInfosByName(int limit, int offset, String chainType, String scenario, String product, String statement) {
        HumanResourceInfos humanResourceInfos = new HumanResourceInfos();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String userName = user.getUserName();
        System.out.println(userName);
        if(Objects.equals(userName, "admin")) {//是系统用户
            humanResourceInfos.setHumanResourceInfos(enterpriseBasicInfoMapper.getHumanResourceInfos(limit, offset, chainType, scenario, product, statement));
            humanResourceInfos.setTotal(enterpriseBasicInfoMapper.getTotalHumanResourceInfos(chainType, scenario, product, statement));
            return humanResourceInfos;
        }
        else {
            humanResourceInfos.setHumanResourceInfos(enterpriseBasicInfoMapper.getHumanResourceInfosByName(limit, offset, chainType, scenario, product, statement,userName));
            humanResourceInfos.setTotal(enterpriseBasicInfoMapper.getTotalHumanResourceInfosByName(chainType, scenario, product, statement,userName));
            return humanResourceInfos;
        }

    }

    public BusinessStatusInfos getBusinessStatusInfosByName(int limit, int offset, String chainType, String scenario, String product, String statement) {
        BusinessStatusInfos businessStatusInfos = new BusinessStatusInfos();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String userName = user.getUserName();
        System.out.println(userName);
        if(Objects.equals(userName, "admin")) {//是系统用户
            businessStatusInfos.setBusinessStatusInfos(enterpriseBasicInfoMapper.getBusinessStatusInfos(limit, offset, chainType, scenario, product, statement));
            businessStatusInfos.setTotal(enterpriseBasicInfoMapper.getTotalBusinessStatusInfos(chainType, scenario, product, statement));
            return businessStatusInfos;
        }
        else {
            businessStatusInfos.setBusinessStatusInfos(enterpriseBasicInfoMapper.getBusinessStatusInfosByName(limit, offset, chainType, scenario, product, statement,userName));
            businessStatusInfos.setTotal(enterpriseBasicInfoMapper.getTotalBusinessStatusInfosByName(chainType, scenario, product, statement,userName));
            return businessStatusInfos;
        }
    }

    public void addHumanResourceInfos(List<HumanResourceInfo> humanResourceInfos) {
        long updateTime = System.currentTimeMillis() / 1000;
        humanResourceInfos.forEach(req -> req.setUpdateTime(updateTime));
        enterpriseBasicInfoMapper.addHumanResourceInfos(humanResourceInfos);
    }

    public HumanResourceInfos getHumanResourceInfos(int limit, int offset, String chainType, String scenario, String product, String statement) {
        HumanResourceInfos humanResourceInfos = new HumanResourceInfos();
        humanResourceInfos.setHumanResourceInfos(enterpriseBasicInfoMapper.getHumanResourceInfos(limit, offset, chainType, scenario, product, statement));
        humanResourceInfos.setTotal(enterpriseBasicInfoMapper.getTotalHumanResourceInfos(chainType, scenario, product, statement));
        return humanResourceInfos;
    }

    public void addBusinessStatusInfos(List<BusinessStatusInfo> businessStatusInfos) {
        long updateTime = System.currentTimeMillis() / 1000;
        businessStatusInfos.forEach(req -> req.setUpdateTime(updateTime));
        enterpriseBasicInfoMapper.addBusinessStatusInfos(businessStatusInfos);
    }

    public BusinessStatusInfos getBusinessStatusInfos(int limit, int offset, String chainType, String scenario, String product, String statement) {
        BusinessStatusInfos businessStatusInfos = new BusinessStatusInfos();
        businessStatusInfos.setBusinessStatusInfos(enterpriseBasicInfoMapper.getBusinessStatusInfos(limit, offset, chainType, scenario, product, statement));
        businessStatusInfos.setTotal(enterpriseBasicInfoMapper.getTotalBusinessStatusInfos(chainType, scenario, product, statement));
        return businessStatusInfos;
    }

    public void addMaterialManagementInfos(List<MaterialManagementInfo> materialManagementInfos) {
        long updateTime = System.currentTimeMillis() / 1000;
        materialManagementInfos.forEach(req -> req.setUpdateTime(updateTime));
        enterpriseBasicInfoMapper.addMaterialManagementInfos(materialManagementInfos);
    }

    public MaterialManagementInfos getMaterialManagementInfos(int limit, int offset, String chainType, String scenario, String product, String statement) {
        MaterialManagementInfos materialManagementInfos = new MaterialManagementInfos();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String userName = user.getUserName();
        System.out.println(userName);

        if(Objects.equals(userName, "admin")) {//是系统用户
            materialManagementInfos.setMaterialManagementInfos(enterpriseBasicInfoMapper.getMaterialManagementInfos(limit, offset, chainType, scenario, product, statement));
            materialManagementInfos.setTotal(enterpriseBasicInfoMapper.getTotalMaterialManagementInfos(chainType, scenario, product, statement));
            return materialManagementInfos;
        }
        else {
            materialManagementInfos.setMaterialManagementInfos(enterpriseBasicInfoMapper.getMaterialManagementInfosByName(limit, offset, chainType, scenario, product, statement,userName));
            materialManagementInfos.setTotal(enterpriseBasicInfoMapper.getTotalMaterialManagementInfosByName(chainType, scenario, product, statement,userName));
            return materialManagementInfos;
        }

    }

    public void addProductionCapacityInfos(List<ProductionCapacityInfo> productionCapacityInfos) {
        long updateTime = System.currentTimeMillis() / 1000;
        productionCapacityInfos.forEach(req -> req.setUpdateTime(updateTime));
        enterpriseBasicInfoMapper.addProductionCapacityInfos(productionCapacityInfos);
    }

    public ProductionCapacityInfos getProductionCapacityInfos(int limit, int offset, String chainType, String scenario, String product, String statement) {
        ProductionCapacityInfos productionCapacityInfos = new ProductionCapacityInfos();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String userName = user.getUserName();
        System.out.println(userName);
        if(Objects.equals(userName, "admin")) {//是系统用户
            productionCapacityInfos.setProductionCapacityInfos(enterpriseBasicInfoMapper.getProductionCapacityInfos(limit, offset, chainType, scenario, product, statement));
            productionCapacityInfos.setTotal(enterpriseBasicInfoMapper.getTotalProductionCapacityInfos(chainType, scenario, product, statement));
            return productionCapacityInfos;
        }
        else {
            productionCapacityInfos.setProductionCapacityInfos(enterpriseBasicInfoMapper.getProductionCapacityInfosByName(limit, offset, chainType, scenario, product, statement,userName));
            productionCapacityInfos.setTotal(enterpriseBasicInfoMapper.getTotalProductionCapacityInfosByName(chainType, scenario, product, statement,userName));
            return productionCapacityInfos;
        }
    }

    public void addSaleCapabilityInfos(List<SaleCapabilityInfo> saleCapabilityInfos) {
        long updateTime = System.currentTimeMillis() / 1000;
        saleCapabilityInfos.forEach(req -> req.setUpdateTime(updateTime));
        enterpriseBasicInfoMapper.addSaleCapabilityInfos(saleCapabilityInfos);
    }

    public SaleCapabilityInfos getSaleCapabilityInfos(int limit, int offset, String chainType, String scenario, String product, String statement) {
        SaleCapabilityInfos saleCapabilityInfos = new SaleCapabilityInfos();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String userName = user.getUserName();
        System.out.println(userName);
        if(Objects.equals(userName, "admin")) {//是系统用户
            saleCapabilityInfos.setSaleCapabilityInfos(enterpriseBasicInfoMapper.getSaleCapabilityInfos(limit, offset, chainType, scenario, product, statement));
            saleCapabilityInfos.setTotal(enterpriseBasicInfoMapper.getTotalSaleCapabilityInfos(chainType, scenario, product, statement));
            return saleCapabilityInfos;
        }
        else {
            saleCapabilityInfos.setSaleCapabilityInfos(enterpriseBasicInfoMapper.getSaleCapabilityInfosByName(limit, offset, chainType, scenario, product, statement,userName));
            saleCapabilityInfos.setTotal(enterpriseBasicInfoMapper.getTotalSaleCapabilityInfosByName(chainType, scenario, product, statement,userName));
            return saleCapabilityInfos;
        }
    }

    public List<String> getChainTypeByName() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String userName = user.getUserName();
        if(Objects.equals(userName, "admin")) {
            return enterpriseBasicInfoMapper.getChainType();
        }
        else {
            return enterpriseBasicInfoMapper.getChainTypeByName(userName);
        }
    }

    public List<String> getScenario(String chainType) {
        return enterpriseBasicInfoMapper.getScenario(chainType);
    }

    public List<String> getProduct(String chainType, String scenario) {return enterpriseBasicInfoMapper.getProduct(chainType, scenario);}

    public void deleteEnterprises(String chainType, String scenario, List<String> enterprises) {
        enterpriseBasicInfoMapper.deleteEnterpriseBasicInfos(chainType, scenario, enterprises);
        enterpriseBasicInfoMapper.deleteHumanResourceInfos(chainType, scenario, enterprises);
        enterpriseBasicInfoMapper.deleteBusinessStatusInfos(chainType, scenario, enterprises);
        enterpriseBasicInfoMapper.deleteMaterialManagementInfos(chainType, scenario, enterprises);
        enterpriseBasicInfoMapper.deleteProductionCapacityInfos(chainType, scenario, enterprises);
        enterpriseBasicInfoMapper.deleteSaleCapabilityInfos(chainType, scenario, enterprises);

        enterpriseBasicInfoMapper.deleteHumanResourceScoreInfos(chainType, scenario, enterprises);
        enterpriseBasicInfoMapper.deleteBusinessStatusScoreInfos(chainType, scenario, enterprises);
        enterpriseBasicInfoMapper.deleteMaterialManagementScoreInfos(chainType, scenario, enterprises);
        enterpriseBasicInfoMapper.deletePriceScoreInfos(chainType,scenario,enterprises);
        enterpriseBasicInfoMapper.deleteQualityScoreInfos(chainType,scenario,enterprises);
        enterpriseBasicInfoMapper.deleteServiceScoreInfos(chainType,scenario,enterprises);
        enterpriseBasicInfoMapper.deleteTechnologyScoreInfos(chainType,scenario,enterprises);
        enterpriseBasicInfoMapper.deleteOverAllScore(chainType,scenario,enterprises);
        enterpriseBasicInfoMapper.deleteByEnterpriseAList(chainType,scenario,enterprises);
        enterpriseBasicInfoMapper.deleteByEnterpriseBList(chainType,scenario,enterprises);
    }

    public List<String> getPartScenario(String chainType, String userName) {
        return enterpriseBasicInfoMapper.getPartScenario(chainType,userName);
    }

    public List<String> getPartProduct(String chainType, String scenario,String enterpriseName) {
        return enterpriseBasicInfoMapper.getPartProduct(chainType, scenario,enterpriseName);
    }

}
